<?php

/**
 *  合同mo'ban 
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 10:02
 */
namespace app\api\controller\basemanage;

use app\api\controller\common\ApiBase;
use PhpOffice\PhpWord\TemplateProcessor;
use app\api\controller\common\CommonPlus;

class Contractmobantype extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        $this->admin = session('admin');
        $this->model = model('Contractmobantype');
    }

    /**
     * 合同模板类型列表页
     */
    public function getList()
    {
        if (request()->isPost()) {
            $where['is_delete']=0;
            $result = $this->model->where($where)->order('id desc')->select();
            if($result){
                return json(array('code' => '1', 'msg' => '获取成功','data'=>$result?$result:array()));
            }else{
                return json(array('code' => '0', 'msg' => '获取失败'));
            }
        }else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 设置新增
     * @return \think\response\Json|void
     */
    public function add()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $contract_name = isset($post['contract_name']) && $post['contract_name'] != '' ? $post['contract_name'] : null;
            $contract_type = isset($post['contract_type']) && $post['contract_type'] != '' ? $post['contract_type'] : null;
            if(!$contract_name){
                return json(array('code' => '0', 'msg' => '请输入类型名称'));
            }
            $data['contract_name']=$contract_name;
            $data['contract_type']=$contract_type;
            $data['create_time']=time();
            $result=$this->model->insertData($data);
            //密码
            
            if($result){
                return json(array('code' => '1', 'msg' => '新增成功'));
            }else{
                return json(array('code' => '0', 'msg' => '获取失败'));
            }
        }else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    /**
     * 设置编辑
     * 
     * @return \think\response\Json|void
     */
    public function edit()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : null;
            $contract_name = isset($post['contract_name']) && $post['contract_name'] != '' ? $post['contract_name'] : null;
            $contract_type = isset($post['contract_type']) && $post['contract_type'] != '' ? $post['contract_type'] : null;
            if(!$id){
                return json(array('code' => '0', 'msg' => '缺少关键主键'));
            }
            if(!$contract_name){
                return json(array('code' => '0', 'msg' => '请输入类型名称'));
            }
            $where['id']=$id;
            $r=$this->model->where($where)->find();
            if(!$r){
                return json(array('code' => '0', 'msg' => '编辑记录不存在'));
            }
            $data['contract_name']=$contract_name;
            $data['contract_type']=$contract_type;
            $data['create_time']=time();
            $result=$this->model->updateBywhere($data,$where);
            if($result){
                return json(array('code' => '1', 'msg' => '新增成功'));
            }else{
                return json(array('code' => '0', 'msg' => '获取失败'));
            }
        }else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 设置删除
     * 
     */
    public function del()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : null;
            if(!$id){
                return json(array('code' => '0', 'msg' => '缺少关键主键'));
            }
            $where['id']=$id;
            $r=$this->model->where($where)->find();
            if(!$r){
                return json(array('code' => '0', 'msg' => '编辑记录不存在'));
            }
            $tem=db('contract_mould_yc')->where(['template_type'=>$id])->find();
            if($tem){
                return json(array('code' => '0', 'msg' => '已关联数据不可删除'));
            }
            $result=$this->model->deleteWhere($where);
            if($result){
                return json(array('code' => '1', 'msg' => '删除成功'));
            }else{
                return json(array('code' => '0', 'msg' => '删除失败'));
            }
        }else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


}
